home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / PlainText / Acurs.c next >
C/C++ Source or Header  |  1993-11-24  |  5KB  |  218 lines

  1. /* acurs.c--  animated cursor handling routines used in Oscilloscope.  These are   */
  2. /* from the article by Malcolm H. Teas in MacTechQuarterly, vol2 number 2 and are  */
  3. /* therefore subject to his copyright.                                             */
  4.  
  5. /* Call InitACurs(theCursorsResourceID) to use an animated cursor.  Then call          */
  6. /* SpinACur(countNumber) over and over to make it spin.  If you pass a zero, it will */
  7. /* increment its count.  ShowACurs and HideACurs do as they say.                     */
  8.  
  9.  
  10. #include <Types.h>
  11. #include <Resources.h>
  12. #include <Memory.h>
  13. #include <Quickdraw.h>
  14. #include <ToolUtils.h>
  15. #include <Events.h>
  16. #include <Desk.h>
  17. #include "acurs.h"
  18.  
  19. #define FALSE 0
  20. #define TRUE !FALSE
  21.  
  22.  
  23. /*---------------------------------- static typedefs --------------------------------*/
  24. typedef struct Acur
  25.     {
  26.     short totalFrames;    /* number of cursor frames */
  27.     short currentFrame;    /* the currently shown frame */
  28.     long frame[32];        /* high word has the ID */
  29.     }Acur;
  30.     
  31. /*---------------------------------- static variables -------------------------------*/
  32.  
  33. static int SpinCount=10;
  34. static long count;
  35. static Acur aCurs;
  36. static Acur *aCursR=&aCurs;
  37. static int calledBefore=FALSE;
  38. static int showACurs=FALSE;
  39. static int aCursGoing=FALSE;
  40.  
  41. /*--------------------------- Externally visible subroutines ------------------------*/
  42. int InitACurs(int id)
  43.     {
  44.     int i;
  45.     Handle cHandle;
  46.     
  47.     /* release any acurs handles we already got */
  48.     if (calledBefore)
  49.         {
  50.         for (i=0;i<aCurs.totalFrames;i++)
  51.             ReleaseResource((Handle)(aCurs.frame[i]));
  52.         }
  53.     
  54.     /* get the acur resource */
  55.     cHandle=GetResource('acur',id);
  56.     if (cHandle==0) return(-1);    /* @#!$$@^*& no resource there!! */
  57.     
  58.     /* load it into the record for this code */
  59.     HLock(cHandle);
  60.     aCursR=(Acur*)*cHandle;
  61.     aCurs.totalFrames=aCursR->totalFrames;
  62.     if (aCurs.totalFrames>32) return(-2); /* something wrong with this resource */
  63.     for (i=0;i<aCursR->totalFrames;i++)
  64.         aCurs.frame[i]=aCursR->frame[i];
  65.     HUnlock(cHandle);
  66.     ReleaseResource(cHandle);
  67.     
  68.     /* now we gotta pick up each of the cursors */
  69.     for (i=0;i<aCurs.totalFrames;i++)
  70.         {
  71.         aCurs.frame[i]=(long)GetCursor(HiWord(aCurs.frame[i]));
  72.         if (aCurs.frame[i]==0) return(-3);
  73.         }
  74.     calledBefore=TRUE;
  75.     aCurs.currentFrame=0;
  76.     aCursGoing=TRUE;
  77.     ShowACurs();
  78.     }
  79.     
  80. int SpinACurs(int increment)
  81.     {
  82.     Cursor *curs;
  83.  
  84.     if (increment!=0)
  85.         count+=increment;
  86.     else
  87.         count++;
  88.     if (count<SpinCount) return(FALSE);
  89.     minimain();     /* take care of update and suspend/resume events */
  90.     
  91.     /* if showing it, unlock the old cursor */
  92.     if (showACurs)
  93.         HUnlock((Handle)(aCurs.frame[aCurs.currentFrame]));
  94.     /* move cursor to the next frame */
  95.     aCurs.currentFrame++;
  96.     if (aCurs.currentFrame>=aCurs.totalFrames) aCurs.currentFrame=0;
  97.     
  98.     /* and show it if necessary */
  99.     if (showACurs)
  100.         {
  101.         HLock((Handle)(aCurs.frame[aCurs.currentFrame]));
  102.         curs=*((CursHandle)(aCurs.frame[aCurs.currentFrame]));
  103.         SetCursor(curs);
  104.         }
  105.     count=0;
  106.     return(TRUE);
  107.     }
  108.     
  109. void ShowACurs(void)
  110.     {
  111.     Cursor *curs;
  112.     
  113.     showACurs=TRUE;
  114.     HLock((Handle)aCurs.frame[aCurs.currentFrame]);
  115.     curs=*((CursHandle)(aCurs.frame[aCurs.currentFrame]));
  116.     SetCursor(curs);
  117.     }
  118.  
  119. void HideACurs(void)
  120.     {
  121.     showACurs=FALSE;
  122.     HUnlock((Handle)(aCurs.frame[aCurs.currentFrame]));
  123.     aCursGoing=FALSE;
  124.     }
  125.  
  126. int ACursGoing(void)
  127.     {
  128.     return(aCursGoing);
  129.     }
  130.  
  131. void SuspendACurs(void)
  132.     {
  133.     showACurs=FALSE;
  134.     HUnlock((Handle)(aCurs.frame[aCurs.currentFrame]));
  135.     }
  136.             
  137.     
  138.     
  139. /*-------------------------------- minimain ------------------------------*/
  140. /*   For handling events while processing, especially in the background   */
  141.  
  142. #ifndef NOMMAIN
  143.  
  144. void minimain()
  145. {
  146.     return;
  147. }
  148.  
  149. #if 0
  150.  
  151. extern void DoUpdates();
  152. extern void DoActivates();
  153. extern void DoSuspend();
  154. extern void DoResume();
  155.  
  156. short gWNEisImplemented=TRUE;
  157. short gInBackground=FALSE;
  158. unsigned long gSleepTime=0;    /* this is for foreground, we'll use more in background */
  159.  
  160.  
  161. void minimain()
  162.    {
  163.     EventRecord minievent;
  164.     
  165.     if (gWNEisImplemented)
  166.         {
  167.         while (WaitNextEvent(activMask+updateMask+app4Mask+mDownMask,&minievent,gSleepTime,0)) 
  168.         /*while (WaitNextEvent(everyEvent,&minievent,gSleepTime,NIL)) */
  169.             {    
  170.             switch (minievent.what)
  171.                 {
  172.                 case updateEvt:
  173.                     {
  174.                     DoUpdates(&minievent);
  175.                     break;
  176.                     }
  177.                 case activateEvt:
  178.                     {
  179.                     DoActivates(&minievent,TRUE);
  180.                     break;
  181.                     }
  182.                 case osEvt:
  183.                     {
  184.                     if (!BitTst((Ptr)minievent.message,suspendResumeMessage))
  185.                         DoSuspend(&minievent);        /* suspend event*/
  186.                     else if (BitTst((Ptr)minievent.message,suspendResumeMessage))
  187.                         DoResume(&minievent);        /* resume event */
  188.                     break;
  189.                     }
  190.                 default:
  191.                     SysBeep(2);
  192.                 }
  193.             }
  194.         }
  195.     else while (GetNextEvent(activMask+updateMask,&minievent))
  196.         {
  197.         SystemTask();
  198.  
  199.         switch (minievent.what)
  200.             {
  201.             case updateEvt:
  202.                 {
  203.                 DoUpdates(&minievent);
  204.                 break;
  205.                 }
  206.             case activateEvt:
  207.                 {
  208.                 DoActivates(&minievent,TRUE);
  209.                 break;
  210.                 }
  211.             }
  212.         }
  213.  
  214.     }
  215.   
  216. #endif
  217. #endif
  218.